package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;


import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author spt
 * @description 预约设置控制器
 * @date
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;


    /**
     * 根据当前月份查询预约数据
     * 1.判断查询条件是否大于10
     * 2.根据查询条件调用查询业务方法listOrderSettings
     * 3.返回数据
     *
     * @param year
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@Param("year") Integer year, @Param("month") Integer month) {
        log.info("预约设置接收到的查询条件:{}", year, month);
        if (month < 10) {
            String date = year + "-0" + month;

            List<OrderSetting> orderSettings = orderSettingService.listOrderSettings(date);

            return new Result(true, MessageConst.ACTION_SUCCESS, orderSettings);
        } else {
            String date = year + "-" + month;

            List<OrderSetting> orderSettings = orderSettingService.listOrderSettings(date);

            return new Result(true, MessageConst.ACTION_SUCCESS, orderSettings);
        }



    }

    /**
     * 设置预约最大人数
     *
     * @param map
     * @return
     */
    @PostMapping("/editNumberByDate")
    public Result updateNumberByDate(@RequestBody Map map) {
        //把接收到的参数都转换成string类型
        String date = map.get("orderDate").toString();
        String num = map.get("number").toString();
        //设置日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //将字符串类型转换成Date
        Date orderDate = null;
        try {
            orderDate = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer number = Integer.parseInt(num);
        orderSettingService.updateNumberByDate(orderDate, number);
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
    }

    /**
     * 文件上传批量进行预约设置
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result updateOrder(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {
        InputStream inputStream = excelFile.getInputStream();
        //创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        //获取工作表，既可以根据工作表的顺序获取，也可以根据工作表的名称获取
        XSSFSheet sheet = workbook.getSheetAt(0);
        //获取当前工作表最后一行的行号，行号从0开始
        int lastRowNum = sheet.getLastRowNum();
        //遍历每一行，除了第一行的列名
        for (int i = 1; i <= lastRowNum; i++) {
            //根据行号获取行对象
            XSSFRow row = sheet.getRow(i);
            //获取当前行的日期和数量
            Date dateCellValue = row.getCell(0).getDateCellValue();
            double numericCellValue = row.getCell(1).getNumericCellValue();
            Integer number = (int) numericCellValue;

            orderSettingService.updateNumberByDate(dateCellValue, number);
        }
        inputStream.close();
        workbook.close();
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }
}
